package com.estate.framework.web.service.job;

import com.estate.framework.web.service.data.ZenuContactsApi;
import com.estate.framework.web.service.data.ZenuListingApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

/**
 * 延时消息，周期消息定时任务
 *
 * @author Simon
 * @since 2021-10-08
 */

@Component
@Order(value = 1)
public class ExcuteContactSchduleTask implements ApplicationRunner {

    @Resource
    @Qualifier("threadPoolTaskScheduler")
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    @Value("${contact.schedule.diff:20000000}")
    private Long scheduleDiff;

    @Resource
    private ZenuContactsApi zenuContactsApi;

    private static final Logger log = LoggerFactory.getLogger(ExcuteContactSchduleTask.class);

    @Override
    public void run(ApplicationArguments args) {

        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//                log.info("客户更新定时任务开始执行，当前时间：{}", currentTime);
                try {
                    //定时
                    zenuContactsApi.saveBatchContacts();

                } catch (Exception e) {
                    String errorMsg = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                    log.error("客户更新定时任务执行异常，当前时间：{}，异常信息：{}", errorMsg, e.getMessage());
                }
                String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//                log.info("客户更新定时任务结束执行，当前时间：{}", endTime);

            }
        };
        //执行任务
        threadPoolTaskScheduler.scheduleWithFixedDelay(timerTask, 70000000);
    }
}
